package com.ruoyi.report.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.XlkjReport;
import com.ruoyi.report.service.IXlkjReportService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 举报投诉Controller
 *
 * @author wang
 * @date 2022-01-15
 */
@RestController
@RequestMapping("/h5/report/report")
public class XlkjReportControllerH5 extends BaseController
{
    @Autowired
    private IXlkjReportService xlkjReportService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询举报投诉列表
     */
//    @PreAuthorize("@ss.hasPermi('report:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjReport xlkjReport)
    {
        if(xlkjReport.getPhone() == null){
            xlkjReport.setStatus("2");
        }
        startPage();
        List<XlkjReport> list = xlkjReportService.selectXlkjReportList(xlkjReport);
        for(XlkjReport report:list){
            if(report.getImg() != null && !report.getImg().equals("")){
                report.setImg(UrlUtils.getUrl() +report.getImg());
            }
            if(report.getVideo() != null && !report.getVideo().equals("")){
                report.setVideo(UrlUtils.getUrl() +report.getVideo());
            }
            report.setUserName(sysUserService.selectUserById(report.getUserId()).getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出举报投诉列表
     */
//    @PreAuthorize("@ss.hasPermi('report:report:export')")
//    @Log(title = "举报投诉", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjReport xlkjReport)
    {
        List<XlkjReport> list = xlkjReportService.selectXlkjReportList(xlkjReport);
        ExcelUtil<XlkjReport> util = new ExcelUtil<XlkjReport>(XlkjReport.class);
        return util.exportExcel(list, "举报投诉数据");
    }

    /**
     * 获取举报投诉详细信息
     */
//    @PreAuthorize("@ss.hasPermi('report:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjReport report = xlkjReportService.selectXlkjReportById(id);
        //多普通路径处理
        if(report.getImg() != null) {
            String[] split = report.getImg().split(",");
            String imgStr = "";
            for(int i = 0;i<split.length;i++){
                imgStr += UrlUtils.getUrl() + split[i];
                if(i != split.length-1){
                    imgStr += ",";
                }
            }
            report.setImg(imgStr);
        }
        return AjaxResult.success(report);
    }

    /**
     * 新增举报投诉
     */
//    @PreAuthorize("@ss.hasPermi('report:report:add')")
//    @Log(title = "举报投诉", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjReport xlkjReport)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(xlkjReport.getPhone());
        xlkjReport.setUserId(sysUser.getUserId());
        if(xlkjReport.getImg() != null && xlkjReport.getImg().trim().length() == 0){
            xlkjReport.setImg(null);
        }
        xlkjReport.setStatus("1");
        return toAjax(xlkjReportService.insertXlkjReport(xlkjReport));
    }

    /**
     * 修改举报投诉
     */
    @PreAuthorize("@ss.hasPermi('report:report:edit')")
    @Log(title = "举报投诉", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjReport xlkjReport)
    {
        return toAjax(xlkjReportService.updateXlkjReport(xlkjReport));
    }

    /**
     * 删除举报投诉
     */
    @PreAuthorize("@ss.hasPermi('report:report:remove')")
    @Log(title = "举报投诉", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjReportService.deleteXlkjReportByIds(ids));
    }
}
